import router from "@/router";
import { useUserStore } from "@/store/modules/user";
import { usePermissionStoreHook } from "@/store/modules/permission";

import NProgress from "nprogress";
import "nprogress/nprogress.css";
NProgress.configure({ showSpinner: false }); // 进度条
const permissionStore = usePermissionStoreHook();
// 白名单路由
const whiteList = ["/login",'/register'];
router.beforeEach(async (to, from, next) => {
	console.log(to)
	console.log(from)
	console.log(next);
	NProgress.start();
	const hasToken = localStorage.getItem("accessToken");
	if (hasToken) {
		if (to.path === "/register") {
			// 如果已登录，跳转首页
			next({ path: "/" });
			NProgress.done();
		} else {
			// 未匹配到任何路由，跳转404
			if (to.matched.length === 0) {
				from.name ? next({ name: from.name }) : next("/404");
			} else {
				next();
			}
		}
	} else {
		// // 未登录可以访问白名单页面
		if(to.fullPath.indexOf('integral')>=0 || whiteList.indexOf(to.path) !== -1){
			next();
		}else if(to.fullPath.indexOf('register') >= 0){
			next(`/register`);
			NProgress.done();
		}else{
			next(`/login`);
			NProgress.done();
		}
		// if (whiteList.indexOf(to.path) !== -1) {
		// 	next();
		// } else {
		// 	next(`/login`);
		// 	NProgress.done();
		// }
	}
});

router.afterEach(() => {
	NProgress.done();
});